#!/usr/bin/env ruby

stats = {}

# the log is like:
# ## hdd:ext4:DWAL:2:bufferedio
# # ncpu secs works works/sec real.sec user.sec nice.sec sys.sec idle.sec iowait.sec irq.sec softirq.sec steal.sec guest.sec user.util nice.util sys.util idle.util iowait.util irq.util softirq.util steal.util guest.util
#
# 2 30.009368 3770498.000000 125644.032224 30.0713 0.37 0 9.88 3.55 42.54 0 0.07 0 0 0.615204 0 16.4276 5.90263 70.7318 0 0.11639 0 0
while (line = $stdin.gets)
  line = line.chomp
  case line
  when /^## (hdd|ssd|nvme|mem)(.*)/
    name = line[3..-1].tr(':', '_')
    cpu = line.split(':')[-2]
  when /^# ncpu/
    subname = line[2..-1].tr('.', '_').split

    # ncpu secs works works/sec
    # 18 0.014712 10032.000000 681889.757833
    datas = $stdin.gets

    # ncpu secs works works/sec
    #
    # 4 0.028158 10070.000000 357624.831309
    datas = $stdin.gets if datas.length == 1

    # make sure the value is corresponding to correct subtest
    next unless datas.split(' ')[0] == cpu

    datas = datas.split
    i = 0
    subname.each do |s|
      # ignore below '-nan'
      # ncpu secs works works/sec
      # 2 0.000000 0.000000 -nan
      stats["#{name}.#{s}"] = datas[i] if datas[i] =~ /^\d/
      i += 1
    end
  end
end

types = {
  # full key
  "ncpu" => "param",
  "secs" => "lat",
  "works" => "gauge",
  "works/sec" => "RATE",

  # split suffix key
  "sec" => "lat",
  "util" => "percent",
}

stats.each do |k, v|
  t = types[k] || types[k.split('.').last]
  puts "#{t}.#{k}: #{v}"
end
